{dede:config.pagesize value='30'/}
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
		<title>联动类型管理</title>
		<link rel="stylesheet" href="/static/web/css/font-awesome.min.css">
		<link rel="stylesheet" href="/static/web/css/bootstrap.min.css">
		<link rel="stylesheet" href="/static/web/css/admin.css">
		<script src="/static/web/js/jquery.min.js"></script>
		<script src="/static/web/js/bootstrap.min.js"></script>
		<script src="/static/web/js/webajax.js"></script>
		<script src="/static/web/js/admin.main.js"></script>
		<script>
			function updateItem(aid) {
				var upenumf = document.getElementById('upenumf');
				upenumf.ename.value = $DE('ename' + aid).value;
				upenumf.disorder.value = $DE('disorder' + aid).value;
				upenumf.aid.value = aid;
				upenumf.submit();
			}
			function GetSelType() {
				var selvalue = $DE('egroup1').options[$DE('egroup1').selectedIndex].value;
				return selvalue;
			}
			function AddType() {
				$DE('edsta').style.display = 'block';
				fetch('stepselect_main.php?action=addnew').then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('系统错误，无法获取数据');
				}).then((d) => {
					$DE('edsta').innerHTML = d;
				}).catch((error) => {
					$DE('edsta').innerHTML = errMsg;
				});
			}
			function AddEnum() {
				var id = GetSelType();
				if (id == 0) {
					ShowMsg("必须选择一个类别");
					return false;
				}
				fetch('stepselect_main.php?action=addenum&id=' + id).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('系统错误，无法获取数据');
				}).then((d) => {
					$DE('edsta').innerHTML = d;
				}).catch((error) => {
					$DE('edsta').innerHTML = errMsg;
				});
				$DE('edsta').style.display = 'block';
			}
			function EditType() {
				var id = GetSelType();
				if (id == 0) {
					ShowMsg("必须选择一个类别");
					return false;
				}
				$DE('edsta').style.display = 'block';
				fetch('stepselect_main.php?action=edit&id=' + id).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('系统错误，无法获取数据');
				}).then((d) => {
					$DE('edsta').innerHTML = d;
				}).catch((error) => {
					$DE('edsta').innerHTML = errMsg;
				});
			}
			function ViewType() {
				var id = GetSelType();
				if (id == 0) {
					ShowMsg("必须选择一个类别");
					return false;
				}
				$DE('edsta').style.display = 'block';
				fetch('stepselect_main.php?action=view&id=' + id).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('系统错误，无法获取数据');
				}).then((d) => {
					$DE('edsta').innerHTML = d;
				}).catch((error) => {
					$DE('edsta').innerHTML = errMsg;
				});
			}
			function DelType() {
				DedeConfirm("您确实要删除这个分类吗").then((v) => {
					var id = GetSelType();
					if (id == 0) {
						ShowMsg("必须选择一个类别");
						return false;
					}
					location.href = 'stepselect_main.php?action=del&id=' + id;
				}).catch((e) => {
					console.log(e)
				});
			}
			function CloseTWin() {
				$DE('edsta').style.display = 'none';
			}
			function ChangePage(sobj) {
				var ntxt = sobj.options[sobj.selectedIndex].text;
				if (sobj.options[sobj.selectedIndex].value == 0) {
					location.href = 'stepselect_main.php';
				} else {
					var ns = ntxt.split('|');
					location.href = 'stepselect_main.php?egroup=' + ns[0];
				}
			}
			function TogSel() {
				var ems = document.getElementsByName('ids[]');
				for (i = 0; i < ems.length; i++) {
					if (ems[i].checked == false) ems[i].checked = true;
					else ems[i].checked = false;
				}
			}
			function DelSel() {
				DedeConfirm("确实要删除这些分类吗").then((v) => {
					document.form1.submit();
				}).catch((e) => {
					console.log(e)
				});
			}
			function ChangePage2(sobj) {
				var nv = sobj.options[sobj.selectedIndex].value;
				if (sobj.options[sobj.selectedIndex].value == 0) {
					location.href = 'stepselect_main.php?egroup=<?php echo $egroup;?>';
				} else {
					location.href = 'stepselect_main.php?egroup=<?php echo $egroup;?>&topvalue=' + nv;
				}
			}
		</script>
	</head>
	<body>
		<table class="table shadow-sm my-3">
			<tr>
				<td>
					<select name="egroup1" onChange="ChangePage(this);" id="egroup1" class="admin-input-sm">
						<option value="0">所有组</option>
						<?php
						$selgroup = '';
						foreach($etypes as $arr)
						{
							$stylecolor = '';
							if ($arr['issystem']==1) $stylecolor = "class='text-black-50'";
							if ($egroup==$arr['egroup']) {
								$selgroup = $arr['itemname'];
								echo "<option value='{$arr['id']}' $stylecolor selected='1'>{$arr['egroup']}|{$arr['itemname']}</option>";
							} else {
								echo "<option value='{$arr['id']}' $stylecolor>{$arr['egroup']}|{$arr['itemname']}</option>";
							}
						}
						?>
					</select>
					<button type="button" name="gedit" id="gedit" class="btn btn-success btn-sm" onclick="EditType();">修改</button>
					<button type="button" name="gview" id="gview" class="btn btn-success btn-sm" onclick="ViewType();">预览</button>
					<button type="button" name="gdel" id="gdel" class="btn btn-danger btn-sm" onclick="DelType();">删除</button>
					<button type="button" name="addnew" id="addnew" class="btn btn-success btn-sm" onclick="AddType();">新增联动组</button>
					<a href="stepselect_main.php?action=upallcache" class="btn btn-success btn-sm">更新所有缓存</a>
				</td>
			</tr>
		</table>
		<?php
		if (!empty($egroup)) {
			$arr = $dsql->GetOne("SELECT * FROM `#@__stepselect` WHERE egroup='{$egroup}' ");
			$dsql->Execute('out',"SELECT evalue,ename FROM `#@__sys_enum` WHERE egroup='{$arr['egroup']}' ORDER BY disorder ASC,evalue ASC");
			$options = '';
			while($row1 = $dsql->GetArray('out'))
			{
				if (!preg_match("#\.#", $row1['evalue'])) {
					$row1['ename'] = ($row1['evalue'] % 500 == 0) ? $row1['ename'] : '└─ '.$row1['ename'];
					if ($topvalue != $row1['evalue']) $options .= "<option value='{$row1['evalue']}'>{$row1['ename']}</option>";
					else $options .= "<option value='{$row1['evalue']}' selected='selected'>{$row1['ename']}</option>";
				}
			}
			//如果添加3级类別
			if ($topvalue % 500 != 0) $arr['issign'] = 2;
		?>
		<table class="table shadow-sm mb-3">
			<tr>
				<td colspan="8"><?php echo $selgroup;?></td>
			</tr>
			<tr>
				<td>
					<form action="stepselect_main.php" method="post">
						<input type="hidden" name="action" value="addenum_save">
						<input type="hidden" name="issign" value="<?php echo $arr['issign'];?>">
						<input type="hidden" name="egroup" value="<?php echo $arr['egroup'];?>">
						<select name="topvalue" onChange="ChangePage2(this);" class="admin-input-sm">
							<option value="0"><?php echo $selgroup;?></option>
							<?php echo $options;?>
						</select>
						<input type="text" name="ename" class="admin-input-lg" placeholder="请填分类名称">
						<button type="submit" name="sb2" class="btn btn-success btn-sm">添加分类</button>
						<?php if ($egroup=='nativeplace') {echo "<a href='stepselect_main.php?action=exarea' class='btn btn-success btn-sm'>导入默认地区</a>";}?>
					</form>
				</td>
			</tr>
		</table>
		<form action="stepselect_main.php" name="upenumf" method="post" id="upenumf">
			<input type="hidden" name="action" value="upenum">
			<input type="hidden" name="aid">
			<input type="hidden" name="ename">
			<input type="hidden" name="disorder">
		</form>
		<form name="form1" action="stepselect_main.php" method="post">
			<input type="hidden" name="action" value="delenumAllSel">
			<table class="table">
				<tr>
					<td colspan="8"><a href="stepselect_main.php">联动类型管理</a> - <a href="stepselect_main.php?egroup=<?php echo $egroup;?>"><?php echo $selgroup;?></a></td>
				</tr>
				<tr align="center">
					<td width="6%">选择</td>
					<td width="6%">id</td>
					<td width="20%">枚举名</td>
					<td width="20%">类别名</td>
					<td width="10%">枚举类型</td>
					<td width="10%">枚举值</td>
					<td width="10%">组内排序</td>
					<td>操作</td>
				</tr>
				{dede:datalist empty='<tr><td colspan="8" align="center">暂无记录</td></tr>'}
				<tr align="center">
					<td><input type="checkbox" name="ids[]" value="{dede:field.id/}"></td>
					<td>{dede:field.id/}</td>
					<td>
						<?php 
						if (!preg_match("#\.#", $fields['evalue'])) {
							if ($fields['evalue']>500 && $fields['evalue']%500 != 0) $fields['ename'] = "└─ ".$fields['ename'];
						} else {
							$fields['ename'] = "└── ".$fields['ename'];
						}
						?>
						<input type='text' id='ename{dede:field.id/}' value='{dede:field.ename/}'>
					</td>
					<td>{dede:field.egroup/}</td>
					<td>
						<?php
						if (!preg_match("#\.#", $fields['evalue'])) {
							if ($fields['evalue']>500 && $fields['evalue']%500 != 0) echo '二级选择';
							else echo '一级选择';
						} else {
							echo "三级选择";
						}
						?>
					</td>
					<td>{dede:field.evalue/}</td>
					<td><input type='text' id='disorder{dede:field.id/}' value='{dede:field.disorder/}'></td>
					<td>
						<?php
						if (!empty($egroup)) {
						?>
						<a href='javascript:updateItem({dede:field.id/});' class='btn btn-success btn-sm'><i class='fa fa-repeat'></i> 更新</a>
						<a href='stepselect_main.php?action=delenum&id={dede:field.id/}' class='btn btn-danger btn-sm'><i class='fa fa-trash'></i> 删除</a>
						<?php
						} else {
							echo "<a href='stepselect_main.php?egroup={$fields['egroup']}'>".$egroups[$fields['egroup']]."</a>";
						}
						?>
					</td>
				</tr>
				{/dede:datalist}
				<tr>
					<td colspan="8">
						<a href="javascript:TogSel();" class="btn btn-success btn-sm">选择</a>
						<a href="javascript:DelSel();" class="btn btn-danger btn-sm">删除</a>
					</td>
				</tr>
				<tr>
					<td colspan="8" align="center">{dede:pagelist listsize='6'/}</td>
				</tr>
			</table>
		</form>
		<?php
		} else {
		?>
		<table class="table shadow-sm mb-3">
			<tr>
				<td colspan="7">联动类型管理</td>
			</tr>
			<tr align="center">
				<td width="6%">选择</td>
				<td width="6%">id</td>
				<td width="30%">类别名</td>
				<td width="10%">级数</td>
				<td width="10%">系统</td>
				<td width="10%">缓存名</td>
				<td>操作</td>
			</tr>
			{dede:datalist}
			<tr align="center">
				<td><input type="checkbox" name="ids[]" value="{dede:field.id/}"></td>
				<td>{dede:field.id/}</td>
				<td><a href="stepselect_main.php?egroup={dede:field.egroup/}">{dede:field.itemname/}</a></td>
				<td>
					<?php
					switch ($fields['issign']) {
						case 0:
							echo "一级选择";
							break;
						case 1:
							echo "二级选择";
							break;
						case 2:
							echo "三级选择";
							break;
					}
					?>
				</td>
				<td> {dede:field.issystem function="@me==1 ? '是' : '否'"/} </td>
				<td>{dede:field.egroup/}</td>
				<td>
					<a href="stepselect_main.php?action=upallcache&egroup={dede:field.egroup/}" class="btn btn-light btn-sm"><i class="fa fa-repeat"></i> 更新</a>
					<a href="stepselect_main.php?egroup={dede:field.egroup/}" class="btn btn-light btn-sm"><i class="fa fa-eye"></i> 查看</a>
				</td>
			</tr>
			{/dede:datalist}
			<tr>
				<td colspan="7" align="center">{dede:pagelist listsize='6'/}</td>
			</tr>
		</table>
		<?php }?>
		<div id="edsta"></div>
	</body>
</html>